{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 一、ndarray 数据类型\n",
    "## 特性\n",
    "1. 多维性"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "6d01778a48fa74b6"
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-09-04T05:31:57.664093700Z",
     "start_time": "2025-09-04T05:31:57.578454500Z"
    }
   },
   "id": "db5cf01ab07bf446"
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "数组维度 0\n"
     ]
    }
   ],
   "source": [
    "# 0维数组\n",
    "arr = np.array(5)\n",
    "print(arr)\n",
    "# 数组的维度\n",
    "print('数组维度: ', arr.ndim)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T05:58:02.756304600Z",
     "start_time": "2025-08-25T05:58:02.712147600Z"
    }
   },
   "id": "f7c158c71ab67097"
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组维度 1\n"
     ]
    }
   ],
   "source": [
    "# 1维数组\n",
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "print(arr)\n",
    "print('数组维度: ', arr.ndim)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T05:59:13.968713600Z",
     "start_time": "2025-08-25T05:59:13.957253100Z"
    }
   },
   "id": "6d124a3c33f6e74c"
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5]\n",
      " [ 6  7  8  9 10]]\n",
      "数组维度 2\n"
     ]
    }
   ],
   "source": [
    "# 2维数组\n",
    "arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])\n",
    "print(arr)\n",
    "print('数组维度: ', arr.ndim)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T05:59:58.054779900Z",
     "start_time": "2025-08-25T05:59:58.040061700Z"
    }
   },
   "id": "566217e4bda30015"
  },
  {
   "cell_type": "markdown",
   "source": [
    "2. 同质性"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "124c5d139f7edf60"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1' '2' 'hello']\n"
     ]
    }
   ],
   "source": [
    "# 同质性：同一个类型元素,会自动完成类型转换\n",
    "arr = np.array([1, 2, 'hello'])  # 会转换称同一种类型\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T06:02:45.579422200Z",
     "start_time": "2025-08-25T06:02:45.563185400Z"
    }
   },
   "id": "4520077583d45699"
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.  2.8]\n"
     ]
    }
   ],
   "source": [
    "# 混合类型，强制转换类型\n",
    "arr = np.array([1, 2.8])  # 会转换称同一种类型\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T06:03:57.352152500Z",
     "start_time": "2025-08-25T06:03:57.341534100Z"
    }
   },
   "id": "488fc5c28b792820"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## ndarray属性"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "52f1d78f6069a0e3"
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组的形状： ()\n",
      "数组的形状： (3,)\n",
      "数组的形状： (3, 3)\n"
     ]
    }
   ],
   "source": [
    "# 数组的形状\n",
    "arr = np.array(1)\n",
    "print('数组的形状：', arr.shape)\n",
    "\n",
    "arr = np.array([1, 2, 3])\n",
    "print('数组的形状：', arr.shape)\n",
    "# 1维元素的长度必须一致，否则报错\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "# arr =np.array([[1,2,3,4],[4,5,6],[7,8,9]]) # 报错\n",
    "print('数组的形状：', arr.shape)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T06:10:30.580310500Z",
     "start_time": "2025-08-25T06:10:30.572100Z"
    }
   },
   "id": "b76c0ca9e3d5ab4f"
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组的大小： 1\n",
      "数组的大小： 3\n",
      "数组的大小： 9\n"
     ]
    }
   ],
   "source": [
    "# 数组的大小\n",
    "arr = np.array(1)\n",
    "print('数组的大小：', arr.size)\n",
    "\n",
    "arr = np.array([1, 2, 3])\n",
    "print('数组的大小：', arr.size)\n",
    "# 1维元素的长度必须一致，否则报错\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "# arr =np.array([[1,2,3,4],[4,5,6],[7,8,9]]) # 报错\n",
    "print('数组的大小：', arr.size)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T06:25:57.049346700Z",
     "start_time": "2025-08-25T06:25:57.040849500Z"
    }
   },
   "id": "5cb88dce22408a99"
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "元素的数据类型： int32\n",
      "元素的数据类型： float64\n",
      "元素的数据类型： <U32\n",
      "元素的数据类型： <U11\n"
     ]
    }
   ],
   "source": [
    "# 元素的数据类型\n",
    "arr = np.array(1)\n",
    "print('元素的数据类型：', arr.dtype)\n",
    "\n",
    "arr = np.array([1, 2, 3, 2.5])\n",
    "print('元素的数据类型：', arr.dtype)\n",
    "\n",
    "arr = np.array([1, 2, 3, 2.5, '合肥'])\n",
    "print('元素的数据类型：', arr.dtype)\n",
    "\n",
    "# 1维元素的长度必须一致，否则报错\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, '合肥']])\n",
    "# arr =np.array([[1,2,3,4],[4,5,6],[7,8,9]]) # 报错\n",
    "print('元素的数据类型：', arr.dtype)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T06:19:09.813882Z",
     "start_time": "2025-08-25T06:19:09.781796600Z"
    }
   },
   "id": "fca042196cf9acd"
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 4 7]\n",
      " [2 5 8]\n",
      " [3 6 9]]\n"
     ]
    }
   ],
   "source": [
    "# 数组转置，行列互转\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "print(arr.T)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T06:24:09.281864200Z",
     "start_time": "2025-08-25T06:24:09.267855700Z"
    }
   },
   "id": "67a9482a4361b40d"
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组维度： 0\n",
      "数组维度： 1\n",
      "数组维度： 2\n"
     ]
    }
   ],
   "source": [
    "# 数组维度\n",
    "arr = np.array(1)\n",
    "print('数组维度：', arr.ndim)\n",
    "\n",
    "arr = np.array([1, 2, 3, 2.5])\n",
    "print('数组维度：', arr.ndim)\n",
    "\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "print('数组维度：', arr.ndim)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T06:26:02.701147100Z",
     "start_time": "2025-08-25T06:26:02.662673300Z"
    }
   },
   "id": "163d8a2b30abb7c6"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## ndarray 的创建"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "3742543bed411f55"
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 2. 3.]\n",
      " [4. 5. 6.]\n",
      " [7. 8. 9.]]\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "# 基础的创建方法\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "# # 指定数据类型\n",
    "# arr = np.array([[1,2,3],[4,5,6],[7,8,9]],dtype=np.float64)\n",
    "print(arr)\n",
    "print(arr.ndim)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T06:27:05.219252Z",
     "start_time": "2025-08-25T06:27:05.213935500Z"
    }
   },
   "id": "1a54e2f673ad4b6"
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 2. 3.]\n",
      " [4. 5. 6.]\n",
      " [7. 8. 9.]]\n",
      "[[100.   2.   3.]\n",
      " [  4.   5.   6.]\n",
      " [  7.   8.   9.]]\n"
     ]
    }
   ],
   "source": [
    "# copy，是深度copy\n",
    "arr1 = np.copy(arr)\n",
    "arr1[0][0] = 100\n",
    "print(arr)\n",
    "print(arr1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T06:28:46.911774800Z",
     "start_time": "2025-08-25T06:28:46.868950Z"
    }
   },
   "id": "83984c4f7f0314c5"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#  全 0 数组\n",
    "# 1维数组\n",
    "arr = np.zeros((5,), dtype=np.int32)  # 全0数组 ，3行3列\n",
    "print(arr)\n",
    "# 2维数组\n",
    "arr = np.zeros((2, 3), dtype=np.int32)  # 全0数组 ，3行3列\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "3f294f8a80ee2227"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 全 1 数组\n",
    "# 1维数组\n",
    "arr = np.ones((5,), dtype=np.int32)  # 全1数组 ，3行3列\n",
    "print(arr)\n",
    "# 2维数组\n",
    "arr = np.ones((2, 3), dtype=np.int32)  # 全1数组 ，3行3列\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a37f83b23c590435"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 未初始数组\n",
    "# 1维数组\n",
    "arr = np.empty((5,), dtype=np.int32)  # 全1数组 ，3行3列\n",
    "print(arr)\n",
    "# 2维数组\n",
    "arr = np.empty((2, 3), dtype=np.int32)  # 全1数组 ，3行3列\n",
    "print(arr)\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f12f0ec59a2cbfda"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 初始化数组\n",
    "# 1维数组\n",
    "arr = np.full((5,), fill_value=100, dtype=np.int32)  # 全1数组 ，3行3列\n",
    "print(arr)\n",
    "# 2维数组\n",
    "arr = np.full((2, 3), fill_value=100, dtype=np.int32)  # 全1数组 ，3行3列\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f5bbfb0288fe222a"
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.47829488 0.43571773 0.64524466]\n",
      " [0.25001764 0.03959913 0.8262249 ]]\n",
      "[[2.98525322 4.90222486 0.31826654]\n",
      " [9.90051389 9.02674384 6.25363984]]\n"
     ]
    }
   ],
   "source": [
    "# 随机 数组,默认0-1之间的浮点数\n",
    "# arr = np.random.random((2, 3))\n",
    "arr = np.random.rand(2, 3)\n",
    "print(arr)\n",
    "\n",
    "# 生成指定范围的随机浮点数，\n",
    "# 指定的范围 ： 0 - 10 间的随机数\n",
    "arr = np.random.uniform(0, 10, (2, 3))\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:26:07.305479Z",
     "start_time": "2025-08-25T07:26:07.266854Z"
    }
   },
   "id": "d8c4e02c905b87ff"
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "source": [
    "# 随机 数组,默认0-1之间的浮点数\n",
    "arr = np.random.randint(2, 9)\n",
    "# arr = np.random.randint(2, 9,size=(2, 3))\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:26:03.056090400Z",
     "start_time": "2025-08-25T07:26:03.048610Z"
    }
   },
   "id": "d7d4543366722f9f"
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.62294368  1.25388874  0.34372223]\n",
      " [ 0.26080934  1.20582015 -1.73581109]]\n"
     ]
    }
   ],
   "source": [
    "# 生成随机数（正态分布）\n",
    "arr = np.random.randn(2, 3)\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:25:57.393394300Z",
     "start_time": "2025-08-25T07:25:57.361789200Z"
    }
   },
   "id": "c241b34c4c126fcc"
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[6 7 6]\n",
      " [4 2 3]]\n"
     ]
    }
   ],
   "source": [
    "# 设置随机数种子,确保每次生成的随机数相同\n",
    "# 种子相同的话，每次生成的随机数相同\n",
    "np.random.seed(203)\n",
    "arr = np.random.randint(2, 9, size=(2, 3))\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:25:43.714157Z",
     "start_time": "2025-08-25T07:25:43.681869400Z"
    }
   },
   "id": "2c4984e901e5c853"
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------like函数------------------------\n",
      "zeros_like: [0 0 0 0 0]\n",
      "ones_like: [1 1 1 1 1]\n",
      "empty_like: [0 0 0 0 0]\n",
      "full_like: [100 100 100 100 100]\n"
     ]
    }
   ],
   "source": [
    "#  like函数 , 创建一个和arr一样形状的数组\n",
    "# 全0数组 ，像arr一样是2行3列\n",
    "arr = np.zeros_like(arr)\n",
    "print('zeros_like:', arr)\n",
    "arr = np.ones_like(arr)\n",
    "print('ones_like:', arr)\n",
    "arr = np.empty_like(arr)\n",
    "print('empty_like:', arr)\n",
    "arr = np.full_like(arr, fill_value=100)\n",
    "print('full_like:', arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T06:44:05.134057Z",
     "start_time": "2025-08-25T06:44:05.124997300Z"
    }
   },
   "id": "52c2e50d3bacb6ce"
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------等差数列------------------------\n",
      "[0 2 4 6 8]\n"
     ]
    }
   ],
   "source": [
    "# 等差数列\n",
    "arr = np.arange(0, 10, 2)  # start , end ,  step\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T06:42:10.974816500Z",
     "start_time": "2025-08-25T06:42:10.923925200Z"
    }
   },
   "id": "216d7ac894261a58"
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 4. 8.]\n"
     ]
    }
   ],
   "source": [
    "# 等间隔数列\n",
    "# 0-10 的5个等间隔数列,即均分5份\n",
    "arr = np.linspace(0, 8, 3)\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T06:50:53.642032200Z",
     "start_time": "2025-08-25T06:50:53.601148300Z"
    }
   },
   "id": "ea4630fb3970050f"
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  1.  16. 256.]\n"
     ]
    }
   ],
   "source": [
    "# 对数等间隔数列\n",
    "# 0-8 等间隔取数（0 ， 4 ，8），然后以2为底取对数，2的0次方，2的4次方，2的8次方\n",
    "arr = np.logspace(0, 8, 3, base=2)\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T06:54:12.001481100Z",
     "start_time": "2025-08-25T06:54:11.982459Z"
    }
   },
   "id": "ff3bebd93ad637d2"
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0 0]\n",
      " [0 1 0 0]\n",
      " [0 0 1 0]]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 矩阵：就是二维数组\n",
    "# 单位矩阵 ：主对角线上的数字是1，其他的数字是0\n",
    "arr = np.eye(3, 4, dtype=np.int32)\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:12:15.219023600Z",
     "start_time": "2025-08-25T07:12:15.163420700Z"
    }
   },
   "id": "a6d8db3f20760ebd"
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 0 0 0]\n",
      " [0 4 0 0]\n",
      " [0 0 5 0]\n",
      " [0 0 0 7]]\n"
     ]
    }
   ],
   "source": [
    "# 对角矩阵 ：主对角线上的数字是非 0 ，其他的数字是0\n",
    "arr = np.diag([3, 4, 5, 7])\n",
    "print(arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:13:58.021742500Z",
     "start_time": "2025-08-25T07:13:57.993103500Z"
    }
   },
   "id": "c4e27909b13daa1d"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## ndarray 索引与切片"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a7d07de2769d22b"
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[74 92 82 22 13 98 54 37 12 19 82 88 41 46 91 26 95 70 97 35]\n"
     ]
    }
   ],
   "source": [
    "# 1维数组的索引与切片\n",
    "arr = np.random.randint(0, 100, 20)\n",
    "print(arr)\n",
    "\n",
    "print(arr[0])\n",
    "\n",
    "print('切片 ：', arr[0:2], arr[slice(0, 2)])\n",
    "\n",
    "print('指定步长切片 ：', arr[slice(0, 5, 2)])\n",
    "\n",
    "print('筛选指定条件的元素（大于50的元素） ：', arr[arr > 50])\n",
    "\n",
    "print('筛选指定条件的元素（大于50且小于70的元素） ：', arr[(arr > 50) & (arr < 70)])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:30:35.141131400Z",
     "start_time": "2025-08-25T07:30:35.121581600Z"
    }
   },
   "id": "ca3a9f6f0070065c"
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[81 46 62 35 61]\n",
      " [13 30 89 63 42]\n",
      " [33 71 33 88 28]\n",
      " [10 52 42 37 14]\n",
      " [30 75 43 62 20]]\n",
      "第2行的第2个元素到第4个元素： [33 88]\n",
      "筛选第3列的数据 ： [35 63 88 37 62]\n",
      "筛选第2行和第4行的第3列的数据 ： [88 37]\n",
      "筛选指定条件的元素（大于50且小于70的元素） ： [62 61 63 52 62]\n",
      "筛选 第二行的 指定条件的元素（大于50的元素） ： [71 88]\n"
     ]
    }
   ],
   "source": [
    "# 2维数组的索引与切片\n",
    "arr = np.random.randint(0, 100, (5, 5))\n",
    "print(arr)\n",
    "\n",
    "print('第2行的第2个元素到第4个元素：', arr[2, 2:4])\n",
    "\n",
    "print('筛选第3列的数据 ：', arr[:, 3])\n",
    "\n",
    "print('筛选第2行和第4行的第3列的数据 ：', arr[2:4, 3])\n",
    "\n",
    "# 会装换成 1维数组\n",
    "print('筛选指定条件的元素（大于50且小于70的元素） ：', arr[(arr > 50) & (arr < 70)])\n",
    "\n",
    "print('筛选 第二行的 指定条件的元素（大于50的元素） ：', arr[2][(arr[2] > 50)])\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T07:47:27.883577200Z",
     "start_time": "2025-08-25T07:47:27.876600400Z"
    }
   },
   "id": "685cef56f9b389cb"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## ndarray 的运算"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ec31e06595e34e12"
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a: [1 2 3] b: [4 5 6]\n",
      "相加 ： [5 7 9]\n",
      "相减 ： [-3 -3 -3]\n",
      "相乘 ： [ 4 10 18]\n",
      "相除 ： [0.25 0.4  0.5 ]\n"
     ]
    }
   ],
   "source": [
    "# 向量的运算\n",
    "a = np.array([1, 2, 3])\n",
    "b = np.array([4, 5, 6])\n",
    "print('a:', a)\n",
    "print('b:', b)\n",
    "print('相加 ：', a + b)\n",
    "print('相减 ：', a - b)\n",
    "print('相乘 ：', a * b)\n",
    "print('相除 ：', a / b)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T08:09:04.172976300Z",
     "start_time": "2025-08-25T08:09:04.162576800Z"
    }
   },
   "id": "5ffbf70d6bd0a976"
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a: [[1 2 3]\n",
      " [4 5 6]]\n",
      "b: [[ 7  8  9]\n",
      " [10 11 12]]\n",
      "矩阵相加 ： [[ 8 10 12]\n",
      " [14 16 18]]\n",
      "矩阵相减 ： [[-6 -6 -6]\n",
      " [-6 -6 -6]]\n",
      "矩阵相乘 ： [[ 7 16 27]\n",
      " [40 55 72]]\n",
      "矩阵相除 ： [[0.14285714 0.25       0.33333333]\n",
      " [0.4        0.45454545 0.5       ]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵的运算\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "b = np.array([[7, 8, 9], [10, 11, 12]])\n",
    "print('a:', a)\n",
    "print('b:', b)\n",
    "print('矩阵相加 ：', a + b)\n",
    "print('矩阵相减 ：', a - b)\n",
    "print('矩阵相乘 ：', a * b)\n",
    "print('矩阵相除 ：', a / b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T08:10:56.851885200Z",
     "start_time": "2025-08-25T08:10:56.816306900Z"
    }
   },
   "id": "29eb6073c2319b50"
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a: [[1 2 3]\n",
      " [4 5 6]]\n",
      "b: 3\n",
      "矩阵相加 ： [[4 5 6]\n",
      " [7 8 9]]\n",
      "矩阵相减 ： [[-2 -1  0]\n",
      " [ 1  2  3]]\n",
      "矩阵相乘 ： [[ 3  6  9]\n",
      " [12 15 18]]\n",
      "矩阵相除 ： [[0.33333333 0.66666667 1.        ]\n",
      " [1.33333333 1.66666667 2.        ]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵与标量的运算\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "b = np.array(3)  # 标量\n",
    "print('a:', a)\n",
    "print('b:', b)\n",
    "print('矩阵相加 ：', a + b)\n",
    "print('矩阵相减 ：', a - b)\n",
    "print('矩阵相乘 ：', a * b)\n",
    "print('矩阵相除 ：', a / b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T08:12:06.116391400Z",
     "start_time": "2025-08-25T08:12:06.084017800Z"
    }
   },
   "id": "ac3e581334f17c22"
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a: [1 2 3]\n",
      "b: [[1]\n",
      " [2]\n",
      " [3]]\n",
      "矩阵相加 ： [[2 3 4]\n",
      " [3 4 5]\n",
      " [4 5 6]]\n",
      "矩阵相减 ： [[ 0  1  2]\n",
      " [-1  0  1]\n",
      " [-2 -1  0]]\n",
      "矩阵相乘 ： [[1 2 3]\n",
      " [2 4 6]\n",
      " [3 6 9]]\n",
      "矩阵相除 ： [[1.         2.         3.        ]\n",
      " [0.5        1.         1.5       ]\n",
      " [0.33333333 0.66666667 1.        ]]\n"
     ]
    }
   ],
   "source": [
    "# 广播机制\n",
    "a = np.array([1, 2, 3])\n",
    "b = np.array([[1], [2], [3]])\n",
    "print('a:', a)\n",
    "print('b:', b)\n",
    "print('矩阵相加 ：', a + b)\n",
    "print('矩阵相减 ：', a - b)\n",
    "print('矩阵相乘 ：', a * b)\n",
    "print('矩阵相除 ：', a / b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T08:14:21.798278800Z",
     "start_time": "2025-08-25T08:14:21.777356200Z"
    }
   },
   "id": "95ab4789560779ae"
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "矩阵相乘 ： [[ 1  4  9]\n",
      " [16 25 36]\n",
      " [49 64 81]]\n",
      "矩阵相乘 ： [[ 30  36  42]\n",
      " [ 66  81  96]\n",
      " [102 126 150]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵的运算\n",
    "a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "\n",
    "print('矩阵相乘 ：', a * b)\n",
    "print('矩阵相乘 ：', a @ b)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T08:24:21.996987400Z",
     "start_time": "2025-08-25T08:24:21.981958700Z"
    }
   },
   "id": "d797c9c499ebbd16"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 二、numpy 的函数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8c77cadb05732247"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 基本函数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "395a4ae4e19950a4"
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16的平方根 ： 4.0\n",
      "[4. 5. 6.]\n",
      "[4. 5. 6.]\n",
      "[[4. 5. 6.]\n",
      " [7. 8. 9.]]\n"
     ]
    }
   ],
   "source": [
    "# 计算平方根，开根号\n",
    "print('16的平方根 ：', np.sqrt(16))\n",
    "\n",
    "arr = np.array([16, 25, 36])\n",
    "print(np.sqrt(arr))\n",
    "\n",
    "print(np.sqrt([16, 25, 36]))\n",
    "\n",
    "print(np.sqrt([[16, 25, 36], [49, 64, 81]]))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-25T08:32:54.040399700Z",
     "start_time": "2025-08-25T08:32:54.035989Z"
    }
   },
   "id": "6fe20b17ba5bb3c8"
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "指数 ： 2.718281828459045\n",
      "指数 ： 7.38905609893065\n"
     ]
    }
   ],
   "source": [
    "# 指数，e^x , 例如 ：e^1 = 2.71828 , e^2 = 7.389\n",
    "print('指数 ：', np.exp(1))\n",
    "print('指数 ：', np.exp(2))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:08:44.933475100Z",
     "start_time": "2025-08-26T01:08:44.927481900Z"
    }
   },
   "id": "8131ac99c7f36285"
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "指数 ： 7.38905609893065\n",
      "对数 ： 2.0\n"
     ]
    }
   ],
   "source": [
    "# 对数\n",
    "# 用log以a为底的c，结果就是b。这可能就是log_a(c) = b\n",
    "# 比如，比如2的3次方是8，所以以2为底的8的对数就是3，log2(8)=3\n",
    "# 自然对数就是以 e 为底，所以 log(e^x) = x\n",
    "# 常用对数：以10为底，log10(x) = y\n",
    "print('指数 ：', np.exp(2))\n",
    "print('对数 ：', np.log(7.38905609893065))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:10:37.170648Z",
     "start_time": "2025-08-26T01:10:37.151528Z"
    }
   },
   "id": "ac694552137de1f8"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正弦值 ： 1.0\n",
      "余弦值 ： 6.123233995736766e-17\n"
     ]
    }
   ],
   "source": [
    "# 正弦值、余弦值，np.pi = 就是π\n",
    "print('正弦值 ：', np.sin(np.pi / 2))\n",
    "print('余弦值 ：', np.cos(np.pi / 2))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:12:39.307004200Z",
     "start_time": "2025-08-26T01:12:39.253960700Z"
    }
   },
   "id": "229bf5a4ebafcb6c"
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "绝对值 ： 3\n",
      "绝对值 ： [3 4 7]\n"
     ]
    }
   ],
   "source": [
    "# 绝对值\n",
    "print('绝对值 ：', np.abs(3 - 6))\n",
    "print('绝对值 ：', np.abs([3, 4, -7]))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:15:05.437491700Z",
     "start_time": "2025-08-26T01:15:05.426846600Z"
    }
   },
   "id": "81b640032d984a79"
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算a的b次幂 ： 8\n"
     ]
    }
   ],
   "source": [
    "# 计算a的b次幂\n",
    "print('计算a的b次幂 ：', np.power(2, 3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:15:47.973093700Z",
     "start_time": "2025-08-26T01:15:47.947475200Z"
    }
   },
   "id": "a6cda74c4fce2040"
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "四舍五入 ： 3.0\n"
     ]
    }
   ],
   "source": [
    "# 四舍五入\n",
    "print('四舍五入 ：', np.round(3.14159))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:15:59.568414500Z",
     "start_time": "2025-08-26T01:15:59.544956900Z"
    }
   },
   "id": "5658bfa822a6ff4c"
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "向上取整 ： 4.0\n"
     ]
    }
   ],
   "source": [
    "# 向上取整\n",
    "print('向上取整 ：', np.ceil(3.14159))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:18:03.202065600Z",
     "start_time": "2025-08-26T01:18:03.176731800Z"
    }
   },
   "id": "d32cbfc7e96e420b"
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "向下取整 ： 3.0\n"
     ]
    }
   ],
   "source": [
    "# 向下取整\n",
    "print('向下取整 ：', np.floor(3.14159))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:18:13.654763200Z",
     "start_time": "2025-08-26T01:18:13.631943200Z"
    }
   },
   "id": "478c304a3745b076"
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "检测缺失值NaN ： [False  True False]\n"
     ]
    }
   ],
   "source": [
    "#检测缺失值NaN,就是空值\n",
    "print('检测缺失值NaN ：', np.isnan(np.array([1, np.nan, 3])))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:18:49.461959500Z",
     "start_time": "2025-08-26T01:18:49.441949200Z"
    }
   },
   "id": "21fb19be16846545"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 统计函数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5003abebb38d8cc"
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组 ： [ 6  9 13  4  2 12  5 10]\n"
     ]
    }
   ],
   "source": [
    "# arr = np.random.randint(1, 20, 8)\n",
    "arr = np.array([6, 9, 13, 4, 2, 12, 5, 10])\n",
    "print('原始数组 ：', arr)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:24:55.954569100Z",
     "start_time": "2025-08-26T01:24:55.944509100Z"
    }
   },
   "id": "666c6cd37f75012d"
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序 ： [ 2  4  5  6  9 10 12 13]\n"
     ]
    }
   ],
   "source": [
    "# 排序\n",
    "print('原始数组：', arr)\n",
    "print('排序 ：', np.sort(arr))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:49:15.544632200Z",
     "start_time": "2025-08-26T01:49:15.529015600Z"
    }
   },
   "id": "51d870d3c78556dd"
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "求和 ： 61\n"
     ]
    }
   ],
   "source": [
    "#求和\n",
    "print('原始数组：', arr)\n",
    "print('求和 ：', np.sum(arr))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:25:12.627983900Z",
     "start_time": "2025-08-26T01:25:12.601111400Z"
    }
   },
   "id": "5335c010369e4a5c"
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均值 ： 7.625\n"
     ]
    }
   ],
   "source": [
    "# 平均值\n",
    "print('原始数组：', arr)\n",
    "print('平均值 ：', np.mean(arr))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:25:43.941118100Z",
     "start_time": "2025-08-26T01:25:43.912362300Z"
    }
   },
   "id": "a285fa94f4691145"
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最大值 ： 13\n",
      "最大值的索引 ： 2\n"
     ]
    }
   ],
   "source": [
    "#最大值\n",
    "print('原始数组：', arr)\n",
    "print('最大值 ：', np.max(arr))\n",
    "print('最大值的索引 ：', np.argmax(arr))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:49:26.274409700Z",
     "start_time": "2025-08-26T01:49:26.261102200Z"
    }
   },
   "id": "138a4d7519af5634"
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小值 ： 2\n",
      "最小值的索引 ： 4\n"
     ]
    }
   ],
   "source": [
    "# 最小值\n",
    "print('原始数组：', arr)\n",
    "print('最小值 ：', np.min(arr))\n",
    "print('最小值的索引 ：', np.argmin(arr))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:49:29.196318200Z",
     "start_time": "2025-08-26T01:49:29.185885100Z"
    }
   },
   "id": "c7d5008deb6ba797"
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中位数 ： 7.5\n",
      "分位数 ： 7.5\n"
     ]
    }
   ],
   "source": [
    "# 中位数 ： 奇数，排序后的中间值；偶数，排序后的中间两个数的平均值\n",
    "print('原始数组：', arr)\n",
    "print('中位数 ：', np.median(arr))\n",
    "\n",
    "# 分位数 ： \n",
    "# 前50%的数据，中间50%的数据，后50%的数据\n",
    "print('分位数 ：', np.percentile(arr, 50))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:51:37.972354600Z",
     "start_time": "2025-08-26T01:51:37.957392900Z"
    }
   },
   "id": "e885a2e153a21158"
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "平均值 ： 7.625\n",
      "偏差 ： [-1.625  1.375  5.375 -3.625 -5.625  4.375 -2.625  2.375]\n",
      "平方 ： [ 2.640625  1.890625 28.890625 13.140625 31.640625 19.140625  6.890625\n",
      "  5.640625]\n",
      "方差 ： 13.734375\n",
      "标准差 ： 3.7059917700933984\n",
      "方差 ： 13.734375\n",
      "标准差 ： 3.7059917700933984\n"
     ]
    }
   ],
   "source": [
    "# 标准差，方差，\n",
    "print('原始数组：', arr)\n",
    "# 方差\n",
    "# 1. 先求平均值\n",
    "avg = np.mean(arr)\n",
    "print('平均值 ：', avg)\n",
    "# 2.再求偏差\n",
    "dev = arr - avg\n",
    "print('偏差 ：', dev)\n",
    "# 3.再求偏差的平方\n",
    "square = np.square(dev)\n",
    "print('平方 ：', square)\n",
    "# 4.偏差平方的平均值\n",
    "var = np.sum(square) / len(arr)\n",
    "print('方差 ：', var)\n",
    "# 5.方差开根号\n",
    "print('标准差 ：', np.sqrt(var))\n",
    "\n",
    "print('方差 ：', np.var(arr))\n",
    "print('标准差 ：', np.std(arr))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:45:01.535414700Z",
     "start_time": "2025-08-26T01:45:01.532056900Z"
    }
   },
   "id": "2da0ecaae5ff9836"
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "累计和 ： [ 6 15 28 32 34 46 51 61]\n",
      "累计积 ： [      6      54     702    2808    5616   67392  336960 3369600]\n"
     ]
    }
   ],
   "source": [
    "# 累计和，累计积\n",
    "print('原始数组：', arr)\n",
    "print('累计和 ：', np.cumsum(arr))  # 两两相加\n",
    "print('累计积 ：', np.cumprod(arr))  # 两两相乘"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:53:40.398555500Z",
     "start_time": "2025-08-26T01:53:40.386780900Z"
    }
   },
   "id": "ed0c649ab89c1b4c"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 比较函数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8669adb3160262dc"
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组： [ 6  9 13  4  2 12  5 10]\n",
      "是否大于 ： [False False  True False False  True False False]\n",
      "是否大于等于 ： [False False  True False False  True False  True]\n",
      "是否小于 ： [ True  True False  True  True False  True False]\n",
      "是否小于等于 ： [ True  True False  True  True False  True  True]\n",
      "是否等于 ： [False False False False False False False  True]\n",
      "两两数组比较 [ True  True False]\n",
      "两两数组比较 [ True  True False]\n"
     ]
    }
   ],
   "source": [
    "# 是否大于\n",
    "print('原始数组：', arr)\n",
    "print('是否大于 ：', np.greater(arr, 10))\n",
    "print('是否大于等于 ：', np.greater_equal(arr, 10))\n",
    "print('是否小于 ：', np.less(arr, 10))\n",
    "print('是否小于等于 ：', np.less_equal(arr, 10))\n",
    "print('是否等于 ：', np.equal(arr, 10))\n",
    "\n",
    "print('两两数组比较', np.array([1, 2, 3]) == np.array([1, 2, 4]))\n",
    "print('两两数组比较', np.equal([1, 2, 3], [1, 2, 4]))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T01:59:13.246996300Z",
     "start_time": "2025-08-26T01:59:13.231384500Z"
    }
   },
   "id": "85fce59b12bc6ce5"
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "逻辑运算 ： [ True False]\n",
      "逻辑运算 ： [ True  True]\n",
      "逻辑运算 ： [False  True]\n",
      "逻辑运算 ： [False  True]\n",
      "逻辑与 ： [ True False False]\n",
      "逻辑或 ： [ True  True  True]\n",
      "逻辑非 ： [False  True False]\n",
      "逻辑异或 ： [False  True  True]\n"
     ]
    }
   ],
   "source": [
    "# 逻辑运算\n",
    "print('逻辑运算 ：', np.logical_and([True, False], [True, True]))\n",
    "print('逻辑运算 ：', np.logical_or([True, False], [True, True]))\n",
    "print('逻辑运算 ：', np.logical_not([True, False]))\n",
    "print('逻辑运算 ：', np.logical_xor([True, False], [True, True]))\n",
    "print('逻辑与 ：', np.logical_and([1, 0, 1], [1, 1, 0]))\n",
    "print('逻辑或 ：', np.logical_or([1, 0, 1], [1, 1, 0]))\n",
    "print('逻辑非 ：', np.logical_not([1, 0, 1]))\n",
    "print('逻辑异或 ：', np.logical_xor([1, 0, 1], [1, 1, 0]))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T02:01:28.696392500Z",
     "start_time": "2025-08-26T02:01:28.673294100Z"
    }
   },
   "id": "c0753e47786f9cd1"
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "检查元素是否为真 ： False\n",
      "检查元素是否为真 ： True\n",
      "检查元素是否为真 ： False\n",
      "检查元素是否为真 ： True\n"
     ]
    }
   ],
   "source": [
    "# 检查元素是否为真\n",
    "print('检查元素是否为真 ：', np.all([True, True, False]))\n",
    "print('检查元素是否为真 ：', np.any([True, True, False]))\n",
    "print('检查元素是否为真 ：', np.all([1, 2, 0]))\n",
    "print('检查元素是否为真 ：', np.any([1, 2, 0]))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T02:02:36.061066600Z",
     "start_time": "2025-08-26T02:02:36.048823400Z"
    }
   },
   "id": "c4efe5d7a41c5e41"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组： [61 95 73 88 55 73 84 99]\n",
      "自定义条件 ： [-1 95 73 88 -1 73 84 99]\n",
      "是否及格 ： ['不及格' '及格' '及格' '及格' '不及格' '及格' '及格' '及格']\n",
      "优良差： ['不及格' '优秀' '及格' '良好' '不及格' '及格' '良好' '优秀']\n",
      "优良差： ['不及格' '优秀' '及格' '良好' '不及格' '及格' '良好' '优秀']\n"
     ]
    }
   ],
   "source": [
    "# 自定义条件\n",
    "# 满足条件时，返回原数组，否则返回-1\n",
    "arr = np.array([61, 95, 73, 88, 55, 73, 84, 99])\n",
    "print('原始数组：', arr)\n",
    "print('自定义条件 ：', np.where(arr > 70, arr, -1))\n",
    "print('是否及格 ：', np.where(arr > 70, '及格', '不及格'))\n",
    "print('优良差：', np.where(arr > 90, '优秀', np.where(arr > 80, '良好', np.where(arr > 70, '及格', '不及格'))))\n",
    "# 这样更好\n",
    "print('优良差：', np.select([arr > 90, arr > 80, arr > 70], ['优秀', '良好', '及格'], default='不及格'))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T02:12:09.342633600Z",
     "start_time": "2025-08-26T02:12:09.330110100Z"
    }
   },
   "id": "91c5b9b4297123ff"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 排序函数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "867ed29bebaf6b4a"
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组： [61 95 73 88 55 73 84 99]\n",
      "排序函数 ： [55 61 73 73 84 88 95 99]\n",
      "排序元素的索引 ： [4 0 2 5 6 3 1 7]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([61, 95, 73, 88, 55, 73, 84, 99])\n",
    "print('原始数组：', arr)\n",
    "print('排序函数 ：', np.sort(arr))\n",
    "print('排序元素的索引 ：', np.argsort(arr))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T02:19:37.346113700Z",
     "start_time": "2025-08-26T02:19:37.331025900Z"
    }
   },
   "id": "6050c8f57998035c"
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数组： [1 2 3 4 5 5 4 3 2 1]\n",
      "去重 ： [1 2 3 4 5]\n"
     ]
    }
   ],
   "source": [
    "# 去重\n",
    "arr = np.array([1, 2, 3, 4, 5, 5, 4, 3, 2, 1])\n",
    "print('原始数组：', arr)\n",
    "print('去重 ：', np.unique(arr))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T02:18:28.974374400Z",
     "start_time": "2025-08-26T02:18:28.942332100Z"
    }
   },
   "id": "ae77d58145cdceec"
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组拼接 ： [1 2 3 4 5 6]\n"
     ]
    }
   ],
   "source": [
    "# 数组拼接\n",
    "arr1 = np.array([1, 2, 3])\n",
    "arr2 = np.array([4, 5, 6])\n",
    "print('数组拼接 ：', np.concatenate((arr1, arr2)))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T02:19:15.638405800Z",
     "start_time": "2025-08-26T02:19:15.607637600Z"
    }
   },
   "id": "5ed8713378bc59d6"
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分割 ： [array([1, 2]), array([3, 4]), array([5, 6])]\n",
      "分割 ： [array([1]), array([2, 3, 4]), array([5, 6])]\n"
     ]
    }
   ],
   "source": [
    "# 分割\n",
    "arr = np.array([1, 2, 3, 4, 5, 6])\n",
    "print('分割 ：', np.split(arr, 3)) # 等分割\n",
    "# print('分割 ：', np.split(arr, 4)) # 报错，无法等分称4份\n",
    "print('分割 ：', np.split(arr, [1, 4])) # 按位置分割"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-08-26T02:22:13.549817500Z",
     "start_time": "2025-08-26T02:22:13.541460700Z"
    }
   },
   "id": "994fdac413b279ba"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "c840ea329b6583c1"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
